/**
 * 
 */
package org.log4jconfig.xml.editor.contentassist.delegate;

import org.eclipse.jdt.core.IRegion;
import org.eclipse.ui.ide.IDE.SharedImages;
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument;
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion;
import org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion;
import org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList;
import org.eclipse.wst.xml.core.internal.document.DocumentImpl;
import org.eclipse.wst.xml.core.internal.parser.regions.AttributeNameRegion;
import org.eclipse.wst.xml.core.internal.text.XMLStructuredDocumentRegion;
import org.eclipse.wst.xml.ui.internal.contentassist.ContentAssistRequest;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * @author Xi
 * 
 */
@SuppressWarnings("restriction")
public class AppenderRefContentAssistDelegatedProcessor extends
		AbstractContentAssistDelegatedProcessor {

	/**
	 * Scan the whole document, looking for all <appender> elements and
	 * proposing their 'name' attribute to the content assistant
	 */
	public void addAttributeValueProposals(
			ContentAssistRequest contentAssistRequest, String attributeName,
			String attributeCurrentValue, String attributeMatchValue)
			throws Exception {
		// FIXME : need to refactor/externalize this code....
		// use an observer/listener ?
		if (attributeName.equals("ref")) {
			// move up to root node of this xml document
			
			Node node = contentAssistRequest.getNode().getParentNode();
			while(!(node.getParentNode() instanceof DocumentImpl)) {
				node = node.getParentNode();
			}
			// scan children nodes from here (current node is <log4j:configuration>)
			NodeList nodeList = node.getChildNodes();
			for(int i = 0; i < nodeList.getLength(); i++) {
				Node item = nodeList.item(i);
				if("appender".equals(item.getNodeName())) {
					String value = item.getAttributes().getNamedItem("name").getNodeValue();
					addCompletionProposal(contentAssistRequest, value, attributeCurrentValue, attributeMatchValue, null);
				}
			}
		}
	}

}
